package top.hmtools.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import top.hmtools.base.StringTools;

/**
 * 操作表名称，变量名称，字段名称的工具
 * @author HyboJ
 * 创建日期：2018-2-1下午7:58:48
 */
public class TableNameTools {

	public static final char UNDERLINE = '_';

	/**
	 * 驼峰格式字符串转换为下划线格式字符串
	 * 
	 * @param param
	 * @return
	 */
	public static String camelToUnderline(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 下划线格式字符串转换为驼峰格式字符串
	 * 
	 * @param param
	 * @return
	 */
	public static String underlineToCamel(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (c == UNDERLINE) {
				if (++i < len) {
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 使用正则表达式将下划线格式字符串转换为驼峰格式字符串
	 * 
	 * @param param
	 * @return
	 */
	public static String underlineToCamelByRegex(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		StringBuilder sb = new StringBuilder(param);
		Matcher mc = Pattern.compile("_").matcher(param);
		int i = 0;
		while (mc.find()) {
			int position = mc.end() - (i++);
			sb.replace(position - 1, position + 1,
					sb.substring(position, position + 1).toUpperCase());
		}
		return sb.toString();
	}
	
	/**
	 * 使用正则表达式将下划线格式字符串转换为驼峰格式字符串，并且首字母大写
	 * @param param
	 * @return
	 */
	public static String underlineToCamelByRegexAndUpcaseFirstChar(String param){
		String result = underlineToCamelByRegex(param);
		if(StringTools.isBlank(result)){
			return result;
		}
		result = result.replaceFirst(result.substring(0, 1), result.substring(0, 1).toUpperCase());
		return result;
	}
	
	/**
	 * 首字母转大写
	 * @param result
	 * @return
	 */
	public static String upcaseFirst(String result){
		return result.replaceFirst(result.substring(0, 1), result.substring(0, 1).toUpperCase());
	}
	
	/**
	 * 根据基本包路径及表格名称，获取Javabean全限定名称
	 * @param basePackagePath
	 * @param tableName
	 * @return
	 */
	public static String getBeanName(String basePackagePath,String tableName){
		return basePackagePath+underlineToCamelByRegexAndUpcaseFirstChar(tableName)+"Bean";
	}
	
	/**
	 * 根据基本包路径及表格名称，获取Dao接口全限定名称
	 * @param basePackagePath
	 * @param tableName
	 * @return
	 */
	public static String getDaoName(String basePackagePath,String tableName){
		return basePackagePath+underlineToCamelByRegexAndUpcaseFirstChar(tableName)+"Dao";
	}

	public static void main(String[] args) {
		String aaa = "app___version_________fld";
		System.out.println(underlineToCamel(aaa));
		System.out.println(underlineToCamelByRegex(aaa));
		System.out.println(underlineToCamelByRegexAndUpcaseFirstChar(aaa));
		aaa = "appVersionFld";
		System.out.println(camelToUnderline(aaa));

	}
}
